package com.middle.experiment01.Controller;

import com.middle.experiment01.Result.PageResult;
import com.middle.experiment01.Result.Result;
import com.middle.experiment01.Service.StudentService;
import com.middle.experiment01.entity.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RequestMapping("/student/common")
@RestController
@Slf4j
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping
    public Result save(@RequestBody Student student){
        log.info("添加课程信息：{}",student);
        studentService.save(student);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable int id){
        log.info("删除学生功能接口");
        studentService.deleteById(id);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("更新学生:{}的信息",student.getName());
        studentService.update(student);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<Student> getById(@PathVariable Long id){
        log.info("当前查询的id:{}",id);
        Student student=studentService.getById(id);
        return Result.success(student);
    }

    @GetMapping("/page")
    public Result<PageResult> pageAllStudent(){
        log.info("查询所有的学生信息");
        List<Student> students=studentService.pageAllStudent();
        return Result.success(new PageResult(students.size(),students));
    }


    //和前面课程查询的一样 也可新建一个中间表..

}
